package com.huarun.common.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * 全局获取HttpServletRequest,HttpServletResponse等相关对象
 * 
 * @author liucao
 */
public class HttpServletUtils {

	/**
	 * 获取Request对象
	 * 
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		return request;
	}

	/**
	 * 获取Response对象
	 * 
	 * @return
	 */
	public static HttpServletResponse getResponse() {
		ServletWebRequest servletWebRequest = new ServletWebRequest(getRequest());
		HttpServletResponse response = servletWebRequest.getResponse();
		return response;
	}

	/**
	 * 获取session对象
	 * 
	 * @return
	 */
	public static HttpSession getSession() {
		HttpSession session = getRequest().getSession();
		return session;
	}

	/**
	 * 获取登录成功后保存在session中的用户名
	 * 
	 * @return
	 */
	public static String getUserName() {
		HttpSession session = getSession();
		if (session != null) {
			String userName = (String) session.getAttribute("userName");
			return userName;
		}
		return null;
	}

	/**
	 * 获取登录成功后保存在session中用户ID
	 * 
	 * @return
	 */
	public static String getUserID() {
		HttpSession session = getSession();
		if (session != null) {
			String userId = (String) session.getAttribute("userId");
			return userId;
		}
		return null;
	}

	/**
	 * 获取登陆成功后用户的CompanyCode
	 * 
	 * @return
	 */
	public static String getCompanyCode() {
		HttpSession session = getSession();
		if (session != null) {
			String companyCode = (String) session.getAttribute("companyCode");
			return companyCode;
		}
		return null;
	}

	/**
	 * 根据attributeName 获取session中的值
	 * <p>
	 * Title: getSessAttribute
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param attributeName
	 * @return
	 */
	public static String getSessAttribute(String attributeName) {
		HttpSession session = getSession();
		if (session != null) {
			return (String) session.getAttribute(attributeName);
		}
		return null;
	}

	/**
	 * 根据attributeName 设置session中的值
	 * <p>
	 * Title: getSessAttribute
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param attributeName
	 * @return
	 */
	public static boolean setSessAttribute(String attributeName, Object attributeValue) {
		HttpSession session = getSession();
		if (session != null) {
			session.setAttribute(attributeName, attributeValue);
			return true;
		}
		return false;
	}

	/**
	 * 根据attributeName 删除session中的值
	 * 
	 * @param attributeName
	 */
	public static void removeSessAttribute(String attributeName) {
		HttpSession session = getSession();
		if (session != null) {
			session.removeAttribute(attributeName);
		}
	}

	/**
	 * 获取登录成功后的propertyId
	 * 
	 * @return
	 */
	public static Integer getPropertyId() {
		HttpSession session = getSession();
		if (session != null) {
			Integer propertyId = 1;
			if (null != session.getAttribute("projectId") && "" != session.getAttribute("projectId")) {
				propertyId = Integer.valueOf(session.getAttribute("projectId").toString());
			}

			return propertyId;
		}
		return null;
	}
	
	/**
	 * 获取登录成功后的propertyCode
	 * 
	 * @return
	 */
	public static String getPropertyCode() {
		HttpSession session = getSession();
		if (session != null) {
			String projectCode = "00001";
			if (null != session.getAttribute("projectCode") && "" != session.getAttribute("projectCode")) {
				projectCode = session.getAttribute("projectCode").toString();
			}
			return projectCode;
		}
		return null;
	}
}
